package com.bsoft.gol.hbs.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import hcn.base.BaseDictionary;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;


/**
 * Created by pangxl on 2019/12/19.
 */
@SsdevDao(serviceId = "baseDictionaryDaoService")
public abstract class BaseDictionaryDAO extends HibernateSupportDelegateDAO<BaseDictionary> {
    public BaseDictionaryDAO() {
        this.setKeyField("dicId");
    }

    @RpcService
    public BaseDictionary saveBaseDictionary(BaseDictionary baseDictionary) {
        return this.save(baseDictionary);
    }

    @RpcService
    @DAOMethod(sql = " from BaseDictionary where dic_code=:dicCode and object_id=:objectId")
    public abstract BaseDictionary getBaseDicByCode(@DAOParam("dicCode") String dicCode, @DAOParam("objectId") String objectId);

    @RpcService
    public List<BaseDictionary> findUnInitDictionaryList(final String objectId) {
        HibernateStatelessResultAction<List<BaseDictionary>> action = new AbstractHibernateStatelessResultAction<List<BaseDictionary>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append("SELECT\n" +
                        "\ta.dic_code dicCode,\n" +
                        "\ta.dic_name dicName,\n" +
                        "\ta.pinyin_code pinyinCode,\n" +
                        "\tb.dic_id dicId\n" +
                        "FROM\n" +
                        "\tbase_dictionary a\n" +
                        "LEFT JOIN base_dictionary b ON a.dic_code = b.dic_code\n" +
                        "AND b.object_type = '2'\n" +
                        "AND b.object_id =:objectId\n" +
                        "WHERE\n" +
                        "\ta.object_type = '1'\n" +
                        "AND a.object_id = 'system'  and b.dic_id is null ");
                Query query = ss.createSQLQuery(hsql.toString());
                query.setParameter("objectId",objectId);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> dbList = query.list();
                List<BaseDictionary> ls = new ArrayList<>();
                for (Map map : dbList) {
                    ls.add(ConversionUtils.convert(map, BaseDictionary.class));
                }
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}